package com.my.biz.service.impl;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.my.api.response.Msg;
import com.my.biz.dao.BotInfoMapper;
import com.my.biz.dao.GroupInfoMapper;
import com.my.biz.domain.GroupInfoDO;
import com.my.biz.service.IGroupInfoService;
import com.my.biz.service.IGroupMemberInfoService;
import com.my.coolq.api.CoolqApi;
import com.my.coolq.api.response.GroupListResponse;
import com.my.core.base.dao.GenericDao;
import com.my.core.base.service.AbstractGenericService;
import com.my.util.MsgUtil;


 /**
 * 
 * @描述：IGroupInfoService 服务实现类
 * 
 * @创建人： ch
 * 
 * @创建时间：2017年07月04日 21:10:39
 * 
 */
@Service("iGroupInfoService")
public class GroupInfoServiceImpl extends AbstractGenericService<GroupInfoDO, Long> implements IGroupInfoService{
	
	private static final Logger LOGGER = LoggerFactory.getLogger(GroupInfoServiceImpl.class);
	
	@Autowired
	private CoolqApi api;
	
	
	
	@Autowired
	private IGroupMemberInfoService groupMemberInfoService;
	
	@Autowired
	private GroupInfoMapper groupInfoMapper;
	
	@Autowired
	private BotInfoMapper botInfoMapper;
	
	@Override
    public GenericDao<GroupInfoDO, Long> getDao() {
        return groupInfoMapper;
    }

	@Override
	public Msg getGroupInfosByUid(String uid) {
		return MsgUtil.success(groupInfoMapper.getGroupInfosByUid(uid));
	}

	@Override
	public Msg addGroupByUid(String uid, String group) {
		if(StringUtils.isBlank(uid)){
			return MsgUtil.error(999, "请先登录");
		}
		Long gid = Long.valueOf(group);
		if(isExists(gid)){
			return MsgUtil.error(400, "该群已有机器人");
		}
		Long botId = botInfoMapper.getMinGroupCountBot();
		
		insert(new GroupInfoDO(uid, gid,botId));
		return MsgUtil.success(botId);
	}

	@Override
	public Boolean isExists(Long group) {
		return groupInfoMapper.isExists(group)>0;
	}

	
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void getGroupAndGroupMemberInfo(Long groupId) {
		try {
			GroupInfoDO g = selectOne(new GroupInfoDO(groupId));
			if(g==null) return;
			List<GroupListResponse> list = api.getGroupList();
			if(CollectionUtils.isEmpty(list)) return;
			for (GroupListResponse group : list) {
				if(ObjectUtils.equals(group.getGroup_id(),groupId)){
					update(new GroupInfoDO(g.getId(),"member",group.getGroup_name()));
					break;
				}
			}
			groupMemberInfoService.addGroupMemberInfo(g.getUid(), groupId);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public Msg inviteBotAddGroup(Long userId, Long groupId) {
		GroupInfoDO g = selectOne(new GroupInfoDO(groupId));
		if(g==null) return MsgUtil.error("请先去群管理平台添加该群");
		if( ObjectUtils.equals(g.getStatus(),Integer.valueOf(2)))return MsgUtil.error("该群已有机器人");
		if(!ObjectUtils.equals(userId, g .getBotId()))return MsgUtil.error("该群已有机器人");
		update(new GroupInfoDO( g.getId() ,Boolean.TRUE,Integer.valueOf(2)));
		botInfoMapper.updateGroupCountByBotId(userId);
		return MsgUtil.success();
	}

	@Override
	public Boolean addFriendByGroupUser() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Boolean isInviteBotAddGroup(Long userId, Long groupId) {
		return selectOne(new GroupInfoDO(groupId,userId))!=null;
	}
	
	
    


}
